package com.JavaBean;


import java.sql.*;

public class BookDAO  {
    private static Connection connection;

    public BookDAO() throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.jdbc.Driver");
        String url = "jdbc:mysql://localhost:3306/test";
        connection= DriverManager.getConnection(url,"Lingqin","123456");
    }

    public BookBean search(String bookId) throws SQLException {
        BookBean bookBean = new BookBean();
        Statement statement = connection.createStatement();
        String sql = "select * from books where bookId="+bookId;
        ResultSet rs = statement.executeQuery(sql);
        if (rs.next()) {
            bookBean.setBookId(rs.getString(1));
            bookBean.setTitle(rs.getString(2));
            bookBean.setAuthor(rs.getString(3));
            bookBean.setPublisher(rs.getString(4));
            bookBean.setPrice(rs.getFloat(5));
        }
        else {
            return null;
        }
        return bookBean;
    }

    public ResultSet result() throws SQLException {
        Statement stmt = connection.createStatement();
        String sql = "select * from books";
        return stmt.executeQuery(sql);
    }
    public boolean insertBook(BookBean book) throws SQLException {
        Statement st = connection.createStatement();
        String sql="insert into books values('"+book.getBookId()+"','"+book.getTitle()+"','"+book.getAuthor()+"','"+book.getPublisher()+"',"+book.getPrice()+")";
        boolean rs=st.execute(sql);
        return !rs;
    }

}
